package cn.zyl.common.redis.aspect;

import cn.zyl.demo.common.boot.annotation.MethodHandleBoot;
import cn.zyl.demo.common.boot.beans.MethodHandleBean;
import cn.zyl.demo.common.boot.operation.AbstractMethodHandleType;
import cn.zyl.demo.common.boot.tools.AnnotationTool;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * MethodHandleAspect类说明: 接口收尾处理切面工具类
 *
 * @author wsz
 * @version v1.0
 * @date 2020-08-04
 */
@Aspect
@Component
public class MethodHandleAspect {
    @Pointcut("@annotation(cn.zyl.demo.common.boot.annotation.MethodHandleBoot)")
    public void annotationPointcut(){}

    /**
     * 环绕切面
     * @param point 环绕通知
     * @return 接口结果
     * @throws Throwable 异常
     */
    @Around("annotationPointcut()")
    public Object aspect(ProceedingJoinPoint point) throws Throwable {
        MethodHandleBoot methodHandle = AnnotationTool.getAnnotation(point, MethodHandleBoot.class);
        AbstractMethodHandleType methodHandleType = methodHandle.typeClass().newInstance();
        MethodHandleBean handleBean = new MethodHandleBean(point,methodHandle);
        handleBean.startRecord(methodHandleType);
        Object result = point.proceed();
        handleBean.endRecord(result);
        //收尾处理
        methodHandleType.getHandler().newInstance().handle(handleBean);
        return result;
    }
}
